package leetcode.leetcode_107;

import java.util.*;

class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        Queue<TreeNode> nodeList = new LinkedList<>();
        nodeList.add(root);
        while(!nodeList.isEmpty()){
            int size = nodeList.size();
            ArrayList<Integer> integers = new ArrayList<>();
            while(size -- >0){
                TreeNode node = nodeList.poll();
                integers.add(node.val);
                if (node.left != null) {
                    nodeList.add(node.left);
                }
                if (node.right != null) {
                    nodeList.add(node.right);
                }
            }
            list.add(integers);
        }
        Collections.reverse(list);
        return list;
    }
}